// packageCommunity/pages/userFind/index.js
import{comments} from "../../../js/modules/mall.js" 
import {router} from '../../../js/router'; 
import BasePage from '../../../js/common/base-page'
import {util,api} from '../../../js/util';
import authorize from '../../../js/modules/authorize.js'
var app = getApp();
BasePage({ 
  // pageKey: "classify", 
  // validateLogin: app.dns.fkId != app.dns.proprietorId,
  /**
   * 页面的初始数据
   */
  data: { 
    tablist: [],
    activeIndex: 0,
    videoList:[],
    _index:0,
    countData:{},//统计数
    imgDomain: api.imgDomain,
    videoDomain:api.videoDomain,
    enableOss: api.enableOss,
    main:{
      ProprietorId:'',
      FKFlag:'',
      FKId:'',
      Proprietor:'',
      ManagerId:'',
      Id:''
    },
    detail_show: '',
    userName :'',
    PageIndex:1,
    PageSize:10,
    hasMoreData:true,
    thumbWidth:800,
    thumbHeight:800,
    fitHeight:750
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {main} = this.data;
    main.ProprietorId = options.mainid;
    main.FKFlag = options.fkflag;
    main.FKId = options.fkid;
    main.Proprietor = options.proprietor;
    main.ManagerId = options.managerid; 
    main.Id=options.postid;//帖子id
    this.setData({
      main:main
    })
    
  },
  /**
  * 切换选项
  */
 swichNav:function(res){
    // console.log(res,'---res');
    var activeIndex = res.currentTarget.dataset.id;
    if (this.data.activeIndex == activeIndex) return;
    this.setData({
      activeIndex: activeIndex,
      hasMoreData:true,
      PageIndex: 1,
      PageSize:this.data.PageSize
    });
    this.getDataList();
  },
  /**
   * 视频播放
   */
  videoPlay:function(e){
    var index = e.currentTarget.id;
    this.setData({_index:index})
    console.log(index,'----');
    //停止正在播放的视频
    var videoContextPrev = wx.createVideoContext(index)
    videoContextPrev.stop();
    setTimeout(function(){
      //将点击视频进行播放
      var videoContext = wx.createVideoContext(index);
      videoContext.play();
    },500)
  },
   /**
   * 统计数
   */
  getDataCount:function(){
    var that = this;
    let {main} = this.data;
    var data = {ProprietorId:main.ProprietorId,FKFlag:main.FKFlag,FKId:main.FKId,ManagerId:main.ManagerId};
    comments.getUserFind(data,function(content){
      let storeInfo = authorize.getEntity();
      if(data.ProprietorId){
        if(data.FKFlag == storeInfo.fkFlag && data.ManagerId == storeInfo.manager.ManagerId){
          content.Content.userDel = true;
        }else{
          content.Content.userDel = false;
        }
      }else{
        content.Content.userDel = true;
      }
      
      that.setData({countData:content.Content,userName:content.Content.Name})
    })
  },
  //关注--粉丝
  bindFollowList:function(e) {
    //0 -- 关注 1---粉丝
    var id = e.currentTarget.dataset.id;
    var {main} = this.data;
    // console.log(main,'----main');
    if(!this.data.countData.userDel)return;
    wx.navigateTo({
      url: '/packageCommunity/pages/follow/index?id='+id+'&proprietor='+main.Proprietor+'&mainid='+main.ProprietorId+'&fkflag='+main.FKFlag+'&fkid='+main.FKId+'&userid='+main.ManagerId+'&username='+this.data.userName
    })  
  },
  //粉丝
  bindFans:function(){
    
  },
  /**
   * 列表
  */
  getDataList:function(){
    var that = this;
    let {main} = this.data;
    var data = {
      Flag:this.data.activeIndex,
      ProprietorId:main.ProprietorId,FKFlag:main.FKFlag,FKId:main.FKId,Proprietor:main.Proprietor,UserId:main.ManagerId,PageIndex:that.data.PageIndex,PageSize:that.data.PageSize
    };
    // console.log(data,'---index');
    comments.getFoundPostList(data,function(content){
      if(content.Success){
        // console.log(content,'---lll');
        let hasMore = content.Content.count > that.data.PageIndex * that.data.PageSize;
        var data = content.Content.data;
        let storeInfo = authorize.getEntity();
        data.forEach(function(item){
          item.Content = item.Content.replace(/<[^>]+>/g, "")
          if(item.Image.indexOf(',') === -1){
            item.Image = item.Image;
          }else{
            item.Image = item.Image.split(',')[0];
          }
          if(item.FKFlag == storeInfo.fkFlag && item.ManagerId == storeInfo.manager.ManagerId){
            item.userDel = true;
          }else{
            item.userDel = false;
          }
        })
        if (that.data.PageIndex > 1) {
          data = that.data.videoList.concat(data);
        }
        // const newList = [].concat(...(Array.from(data.reduce((total, cur, index) => {
        //   total[index % 2].push(cur)  
        //   return total
        // }, { 0: [], 1: [], length: 2 })))) 
        that.setData({
          videoList:data,
          hasMoreData: hasMore,
        })
      }else{
        util.alert(content.Message);
      }
    })
  },
  //关注--取消关注
  bindFollow:function(e){
    var that = this;
    var status = e.currentTarget.dataset.status;
    let {main} = this.data;
    var data = {FollowFkFlag:main.FKFlag,FollowFkId:main.FKId,FollowManagerId:main.ManagerId}
    var info = that.data.countData;
    // console.log(status,'=====status');
    if(status){//true
      comments.getUserCancelFollow(data,function(content){
        if(content.Success){
          // info.IsFollow = !info.IsFollow;
          that.setData({
            'countData.IsFollow':!info.IsFollow
          })
          util.tips('取消关注成功')
          //统计数
          that.getDataCount();
        }else{
          util.tips(content.Message);
        }
      })

    }else{//false
      comments.getUserFollowAdd(data,function(content){
        if(content.Success){
          // info.IsFollow = !info.IsFollow;
          that.setData({
            'countData.IsFollow':!info.IsFollow
          })
          util.tips('关注成功')
          //统计数
          that.getDataCount();
        }else{
          util.alert(content.Message);
        }
      })
    }
  },
  /**
  * 发布
  */
  btnRelease:function(){
    wx.navigateTo({
      url: '/packageCommunity/pages/releaseImg/index?type=1'
    })
    // if (this.data.detail_show == 'open') {
    //   this.setData({
    //     detail_show: ''
    //   })
    // } else {
    //   this.setData({
    //     detail_show: 'open'
    //   })
    // }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //统计数
    this.getDataCount();
    //列表
    this.getDataList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    if (this.data.detail_show == 'open') {
      this.setData({
        detail_show: ''
      })
    }
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      var index = this.data.PageIndex;
      this.setData({
        PageIndex:index+1
      },function(){
        this.getDataList();
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 跳转
   */
  bindList:function(e){
    //type = 1 图文  type=2 视频
    var id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    if(type == 2){
      router.navigate({
        path: "videoDetail",
        params: {id: id}
      })
    }else{
      router.navigate({
        path: "postdetail",
        params: {id: id}
      })
    }
  },
  /*删除帖子*/
  bindDel(e){
    var that=this;
    var data={Id:e.currentTarget.dataset.id}
    wx.showModal({
      title: '温馨提示',
      content: '是否删除改帖子？',
      success: function(res) {
        if (res.confirm) {
          comments.getDelPost(data,function(res){
            that.getDataList();
            //统计数
            that.getDataCount();
          })
        }
      }
    })
  }
})